<template>

    <u-modal v-model="toVisibleVerifyNum" show-cancel-button confirm-text="确认申请" :async-close="true" ref="uModalApply"
             @confirm="toApplyTrendsDownloadNum()" title="下载次数申请">
      <!--      <view style="margin: 16px">-->
      <!--        <view style="margin-top: 15px;">-->
      <!--          <view class="item">-->
      <!--            <picker mode="date" :value="chooseUseTime" @change="getChooseTime" :start="startDate">-->
      <!--              <view class="desc">{{ chooseUseTime }}</view>-->
      <!--            </picker>-->
      <!--          </view>-->
      <!--        </view>-->
      <!--      </view>-->
      <view style="margin: 10px">
        <u-form :model="formApply" ref="uForm" style="margin: 10px">
          <u-form-item label="申请次数" prop="applyNum" :required="true" label-width="100px">
            <u-input v-model="formApply.applyNum" type="number" />
          </u-form-item>
          <u-form-item label="素材类型"  :required="true" label-width="100px">
            <u-radio-group v-model="formApply.materialType">
              <u-radio v-for="(item, index) in materialTypeRadioList" :key="index" :name="item.materialType" :disabled="item.disabled">
                {{ item.name }}
              </u-radio>
            </u-radio-group>
          </u-form-item>
        </u-form>
      </view>


    </u-modal>



</template>

<script>
const API = require('@/utils/api/find.js').default;
const $ = require('../../utils/api.js');

export default {

  name: "downloadApply",

  data(){
    return{
      toVisibleVerifyNum:false,
      rules: {
        applyNum: [
          { required: true, message: '请输入次数', trigger: 'blur' },
          {
            validator (rule, value, callback) {
              if (/(^[1-9]\d*$)/.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正整数'))
              }
            },
            trigger: 'blur'
          }
        ],
      },
      formApply:{
        applyNum:"",
        materialType:1
      },
      materialTypeRadioList:[
        {
          name: '短视频',
          disabled: false,
          materialType:1
        },
        {
          name: '图片',
          disabled: false,
          materialType:3
        }
      ],
    }
  },
  onLoad() {

  },
  methods:{
    init(){
      this.formApply.applyNum=0
      this.toVisibleVerifyNum=true

      this.$nextTick(()=>{
        this.$refs.uForm.setRules(this.rules);
        this.$refs.uForm.resetFields();
      })
    },
    toApplyTrendsDownloadNum() {
      this.$refs.uModalApply.clearLoading();
      this.$refs.uForm.validate(valid => {
        if (valid) {
          this.addVerifyTrendsDownloadNum()
          this.toVisibleVerifyNum = false
        }
      });

    },
    addVerifyTrendsDownloadNum() {

      const self = this
      $.ajax({
        url: API.addVerifyTrendsDownloadNum,
        data: {
          // "trendsId": this.currentItem.id,//这里不用区分具体的是当前商品
          "num": self.formApply.applyNum,
          "type": self.formApply.materialType,
        },
        loading:true,
        method: 'POST',
        success(res) {
          self.toVisibleVerifyNum=false
          self.visible=false
          $.$toast('已生成申请记录');
        }

      })
    },
  }
}
</script>



<style scoped>

</style>
